package com.houbank.demo.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.houbank.demo.rabbitmq.person.model.PersonDO;
import com.houbank.demo.rabbitmq.person.model.User;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.UUID;


@RestController
public class TestController {
    public org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping(value = "/send", method = RequestMethod.GET)
    public String testSend()throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = objectMapper.writeValueAsString(new PersonDO("1", "10086", "liubo", 20));

        User user = new User();
        user.setUpdateDate(new Date());
        String userJson =objectMapper.writeValueAsString(user);
        String json2 = JSON.toJSONString(new PersonDO("1", "10086", "liubo", 20));
        logger.info(jsonStr+"============================"+userJson);
        Message message = MessageBuilder.withBody(json2.getBytes()).setMessageId(UUID.randomUUID().toString()).build();
        //1、发送消息
        // rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE, AmqpConfig.ROUTINGKEY, message);
        //2、发送消息
         CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());//消息id

        //rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE, AmqpConfig.ROUTINGKEY,new PersonDO("1", "10086", "liubo", 20),correlationId);

        HashMap<String,String> map = new HashMap<String,String>();
        map.put("12","33");
        map.put("13","44");
        rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE, AmqpConfig.ROUTINGKEY,map,correlationId);
        rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE, AmqpConfig.ROUTINGKEY_FAIL,map,correlationId);
        return "ok";
    }
}
